<?php
/*
 * 计划任务
 */
namespace Mobile\Controller;


class CronController extends BaseController {
    public $memberModel;
    public $moneylogModel;
    public $orderModel;
    public $datalogModel;
    public $teamModel;
    public function __construct() {
        session('jiesuan',true);
        parent::__construct();
        $this->memberModel = new \Mobile\Model\memberModel;
        $this->moneylogModel = new \Mobile\Model\moneylogModel;
        $this->orderModel = new \Mobile\Model\orderModel;
        $this->datalogModel = M('datalog');
        $this->teamModel = M('team');
        layout(false);
    }

    /*
     * 月度总代奖励，次月1日执行
     */
    public function monthJiesuan(){
        //datalog  保存执行的月份
        $dataWhere = array(
            'logdate' => date('Y-m'),
            'stype' => 'monthjiesuan'
        );
        $res = $this->datalogModel->where($dataWhere)->find();
        if($res){
            echo '本月已执行';
            exit;
        }else{
            $this->datalogModel->add($dataWhere);
        }
        
        $time = strtotime(date('Y-m-01'));
        $start = date('Y-m-d 00:00:00', strtotime('-1 month',$time));
        $end = date('Y-m-d 00:00:00', strtotime('+0 month',$time));
        $list = $this->memberModel->where(array('grade_id'=>5))->field('uid,grade_id,refer_uid')->select();
        foreach ($list as $member) {
            $mmoney = monthYeji($member['uid'], $start, $end);
            if($mmoney>0) {
                $this->_addMoneyLog($member['uid'], $mmoney, '月度返利', '', '', '',1);
            }
        }

        $this->datalogModel->where($dataWhere)->setField('status',1);
        echo 'ok';
    }
    
    /*
     * 本人
     */
    protected function _getMyMoney($uid) {
        $start = date('Y-m-01 00:00:00', strtotime('-1 month -1 day'));
        $end = date('Y-m-01 00:00:00');
        $where = array(
            'checked' => array('in',array(1,2,5)),
            'uid' => $uid,
            'order_type' => array('in',array(1,2)),
            'pay_time' => array('between',array($start,$end))
        );
        $all = $this->orderModel->where($where)->sum('order_cashmoney');
        return $all>0?$all:0;
    }
    
    /*
     * 团队补货和报单
     */
    protected function _getTeamMoney ($uid) {
        //$all = $this->_getMyMoney($uid);
        $all = 0;
        $xiajilist = $this->memberModel->where(array('refer_uid'=>$uid))->field('uid,refer_uid')->select();
        while(!empty($xiajilist)) {
            $uids = array();
            foreach ($xiajilist as $k=>$v) {
                $uids[] = $v['uid'];
                $all += $this->_getMyMoney($v['uid']);
            }
            $xiajilist = $this->memberModel->where(array('refer_uid'=>array('in',$uids)))->field('uid,refer_uid')->select();
        }
        return $all>0?$all:0;
    }




    /*
     * 写佣金记录
     * $uid 用户uid
     * $money 佣金金额
     * $content 佣金说明
     * $order 订单数据
     * $from_jibie 来自于第几级别
     分佣的比率
     * $mtype 奖励类型
     */
    protected function _addMoneyLog($uid,$money,$content,$order,$from_jibie,$bili=0,$mtype){
        
        //全部发放到佣金账号
        $yongjin_money = round($money,2);
        
        //判断是否锁定佣金
        $shuoding = C('YONGJIN_LOCK');
        if($shuoding == 0){
            //立即发放
            $money_data = array(
                "money" => array('exp',"money+".$yongjin_money),//佣金
            );
            
        }else if($shuoding>0){
            //发放到锁定佣金里面
            $money_data = array(
                "lock_money" => array('exp',"lock_money+".$yongjin_money),//锁定佣金
            );
        }
        
        
        $res = $this->memberModel->where(array('uid'=>$uid))->save($money_data);
        if(!$res){
            return false;
        }
    
        //发给对象的信息
        $myinfo = $this->memberModel->where(array('uid'=>$uid))->find();
    
        //写佣金流水
        $this->_writeMoneylog($uid,$yongjin_money,$content,$order,$from_jibie,$bili,$mtype,$myinfo,$shuoding);
        
    }
    
    
    /*
     * 写收入流水
     * $mtype 1分销佣金,2重消币 3现金电子币 4重消佣金5互助奖6车房奖7领导奖8分红奖 9保证金 10个税
     */
    protected function _writeMoneylog($uid,$money,$content,$order,$from_jibie,$bili=0,$mtype,$myinfo,$shuoding){
        
        if($money == 0){
            return false;//金额等于0 不做任何操作
        }
        
        $realname = $myinfo['realname'] ? $myinfo['realname'] : $myinfo['nickname'];
        $yue = $myinfo["money"]+$myinfo["lock_money"];
        $yue = round($yue,2);

        $money_log = array(
            "uid" => $myinfo['uid'],
            "nickname" => $realname,
            "logtype" => 1,//收入
            "mtype" => $mtype,//类型
            "money" => $money,
            "content" => $content,
            "order_id" => $order['order_id'],
            "order_uid" => $order['uid'],
            "order_paymoney" => $order['order_cashmoney'],//订单支付的金额(需要分佣的金额)
            "order_nickname" => $order['nickname'],
            "pay_time" => $order['pay_time'],//订单支付时间
            "create_time" => date("Y-m-d H:i:s"),//写流水的时间
            "from_jibie" => $from_jibie,
            "bili" => $bili,
            "cash_money" => $yue,//佣金余额,锁定佣金+可提佣金
        );
        
        if($shuoding == 0){
            //不锁定
            $money_log['un_lock'] = 1;
        }else{
            //锁定
            $money_log['un_lock'] = 0;
        }
        
        $res = $this->moneylogModel->addMoneylog($money_log);
        
        return $res;
    }
    
    public function cronFugou () {
        if(date('d')!=1) {
            exit('1号执行');
        }
        $where = array(
            'grade_id' => array('egt',4)
        );
        $start = date('Y-m-00 00:00:00', strtotime('-1 month'));
        $end = date('Y-m-00 00:00:00');
        
        $memberlist = $this->memberModel->where($where)->field('uid,grade_id')->select();
        foreach ($memberlist as $member){
            $orderWhere = array(
                'uid' => $member['uid'],
                'checked' => array('in',array(1,2,5)),
                'order_type' => 2
            );
            $num = $this->orderModel->where($orderWhere)->sum('sj_gnums');
            if($num>=50) {
                $this->memberModel->where(array('uid'=>$member['uid']))->setField('is_fugou',1);
            }else{
                $this->memberModel->where(array('uid'=>$member['uid']))->setField('is_fugou',0);
            }
        }
        echo 'success';
    }
	
}